package cn.edu.neusoft.wuguangsheng.market.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.adapter.SearchAdapter;
import cn.edu.neusoft.wuguangsheng.market.bean.FoodByShopBean;
import cn.edu.neusoft.wuguangsheng.market.listener.ListListener;
import cn.edu.neusoft.wuguangsheng.market.model.SearchModel;

public class SearchinfoActivity extends BaseActivity {

    RecyclerView cecycle1;
    String search;
    ImageView search_btn1;
    private List<FoodByShopBean> items;

    EditText search_et1;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_searchinfo);
        cecycle1=(RecyclerView)findViewById(R.id.cecycle1);
        search_et1=(EditText)findViewById(R.id.search_et1);
        search_btn1=(ImageView)findViewById(R.id.search_btn1);

    }

    @Override
    void initEvent() {
        search_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search=search_et1.getText().toString();

                if(search.equals("")){
                    Toast.makeText(SearchinfoActivity.this,"搜索信息不能为空",Toast.LENGTH_SHORT).show();
                }
                else{
                    SearchModel searchModel=new SearchModel();

                    searchModel.searchIface(search,listListener);

                    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(SearchinfoActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    cecycle1.setLayoutManager(linearLayoutManager);

                }

            }
        });
    }

    @Override
    void initDate() {

    }

    ListListener<FoodByShopBean> listListener=new ListListener<FoodByShopBean>() {
        @Override
        public void onResponse(List<FoodByShopBean> list) {
            items=list;
            if (items.equals("")){
                Toast.makeText(SearchinfoActivity.this,"没有查询到你要搜索的菜品",Toast.LENGTH_SHORT).show();
            }


            else{
                SearchAdapter searchAdapter=new SearchAdapter(SearchinfoActivity.this,items,R.layout.searchitem);
                cecycle1.setAdapter(searchAdapter);
                cecycle1.setItemAnimator(new DefaultItemAnimator());
            }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(SearchinfoActivity.this,"没有查询到你要搜索的菜品",Toast.LENGTH_SHORT).show();
        }
    };
}
